<?php
/**
 * Each engineer has a duty to keep the code elegant
 * Created by wolffy.
 * Author @wolffy<13750088090@sina.cn>
 * Date: 2018/8/11
 * Time: 23:43
 */
namespace App\Http\Controllers\Api\V1_8_8;

use App\Http\Requests\Api\V1_8_8\ModifyHeadImageRequest;
use App\Http\Requests\Api\V1_8_8\ModifyPasswordRequest;
use App\Http\Requests\Api\V1_8_8\UserForgetPayPasswordRequest;
use App\Repositories\UserLevelRelationsRepository;
use App\Repositories\UserRepository;
use App\Services\UserService;
use Illuminate\Support\Facades\Auth;

class AuthController extends BaseController
{
    public function getIkefuUrlMobile($userId, $userName)
    {
        $time = time();
        $params = [
            'userId' => $userId,
            'userName' => $userName,
            'timestamp' => $time,
            'secureKey' => 'd70ed546600b4d2dafb2efbdecfe5b4e',
        ];
        ksort($params);
        $res = '';
        foreach ($params as $value) {
            $res .= $value;
        }
        $res = md5($res);
        $url = 'https://ikefu.baidu.com/wise/kisbean?userId=' . $userId . '&userName=' . $userName .
            '&timestamp=' . $time . '&signature=' . $res;
        return $url;
    }
    /**
     * 获取用户信息接口
     */
    public function getUserInfo(UserLevelRelationsRepository $userLevelRelationsRepository, UserRepository $userRepository)
    {
        $user = $this->user;
        $levelRelations = $userLevelRelationsRepository->first($user['user_id']);
        if (!empty($levelRelations)) {
            if ($levelRelations->pid > 0) {
                $upLevel = $userLevelRelationsRepository->byId($levelRelations->pid);
                $upUser = $userRepository->byId($upLevel->user_id);
                $user['invited_mobile'] = $upUser->mobile;
            } else {
                $user['invited_mobile'] = '';
            }
        }
        //是否有设置支付密码
        $user['is_has_paypwd'] = empty($user['paypwd']) ? 0 : 1;
        $user['ikefu_init_url'] = $this->getIkefuUrlMobile($user['invited_code'], $user['realname']);
        return $this->responseJson(0, $user);
    }

    /**
     * 退出登录接口
     */
    public function logout()
    {
        if (Auth::guard('api')->check()) {
            Auth::guard('api')->user()->token()->revoke();
            return $this->responseJson(0);
        } else {
            return $this->responseJson(403);
        }
    }

    /**
     * 修改登录密码
     */
    public function modifyPassword(ModifyPasswordRequest $request, UserService $userService)
    {
        return $userService->modifyLoginPassword($this->user['user_id'], $request->old_password, $request->password);
    }

    /**
     * 修改支付密码
     */
    public function modifyPayPassword(ModifyPasswordRequest $request, UserService $userService)
    {
        return $userService->modifyPayPassword($this->user['user_id'], $request->old_password, $request->password, $this->user['mobile']);
    }

    /**
     * 忘记支付密码
     */
    public function forgetPayPassword(UserForgetPayPasswordRequest $request, UserService $userService)
    {
        return $userService->forgetPayPassword($request->mobile, $request->password, $request->verification_code, $this->user['mobile']);
    }

    /**
     * 修改头像
     */
    public function modifyHeadImage(ModifyHeadImageRequest $request, UserService $userService)
    {
        return $userService->modifyHeadImage($request, $this->user['user_id']);
    }
}